using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace VoxelEngine.TerrainGenerator
{
	[Serializable]
	public class PlaneTerrainGenerator : TerrainGenerator
	{
		public override IEnumerator CreateWorldFromSeed(World world)
		{
			_voxelProps = new Dictionary<VoxelProp.PropType, VoxelProp>
			{
				{
					VoxelProp.PropType.Tree,
					new TreeProp()
				}
			};
			int height = 4;
			UnityEngine.Debug.Log("Creating World Data");
			for (int x = 0; x < world.WorldVisibleSizeX; x++)
			{
				for (int i = 0; i < world.WorldVisibleSizeZ; i++)
				{
					for (int j = 0; j < world.WorldVisibleSizeY; j++)
					{
						CreateColumn(world, x, j, i, height);
					}
				}
				if (x % 5 == 0)
				{
					yield return null;
				}
			}
		}

		public override void CreateColumn(World world, int x, int y, int z, int surfaceHeight)
		{
			if (y <= 0)
			{
				world[x, y, z] = new Block(BlockType.Lava, x, y, z);
			}
			else if (y == 1)
			{
				world[x, y, z] = new Block(BlockType.Stone, x, y, z);
			}
			else if (y >= 2 && y <= _dirtHeight)
			{
				world[x, y, z] = new Block(BlockType.Dirt, x, y, z);
			}
			else if (y == _dirtHeight + 1)
			{
				world[x, y, z] = new Block(BlockType.Grass, x, y, z);
				_voxelProps[VoxelProp.PropType.Tree].TryAdd(x, y, z);
			}
			else if (y == _dirtHeight + 2)
			{
				world[x, y, z] = new Block(BlockType.Height, x, y, z);
			}
			else
			{
				world[x, y, z] = new Block(BlockType.Air, x, y, z)
				{
					Light = byte.MaxValue
				};
			}
		}
	}
}
